package com.kiln.juxingqs.ui

import android.content.Intent
import android.content.IntentFilter
import android.view.View
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.GoodsDetail
import com.kiln.juxingqs.api.bean.TradeGoodsDetail
import com.kiln.juxingqs.databinding.ActivityLotDealBinding
import com.kiln.juxingqs.dialog.JyInputNumDialog
import com.kiln.juxingqs.receiver.ZrGDListBroadcast
import com.kiln.juxingqs.ui.vm.LotDealModel
import java.util.ArrayList

/**
 * 商品买卖
 */
class LotDealActivity : BaseMiddleActivity<LotDealModel, ActivityLotDealBinding>() {

    var tGoodsDetail: TradeGoodsDetail? = null
    lateinit var bannerList: ArrayList<GoodsDetail.Banner>
    lateinit var detailList: ArrayList<GoodsDetail.Detail>
    var goods_id: String? = ""

    override fun getLayout(): Int {
        return R.layout.activity_lot_deal
    }

    override fun initViewModel(): LotDealModel = createViewModel()

    val meBroadcast: ZrGDListBroadcast by lazy {
        ZrGDListBroadcast()
    }

    override fun initView() {
        binding.vm = viewModel
//        binding.event = this
        binding.refreshLayout.setOnRefreshListener {
            getData()
        }
        binding.emptyLayout.setOnClickListener {
            getData()
        }

        registerReceiver(meBroadcast, IntentFilter(ZrGDListBroadcast.REFREASH_GUADAN_LIST))

//        bannerList = intent.getParcelableArrayListExtra<GoodsDetail.Banner>("banner_list")!!
//        detailList = intent.getParcelableArrayListExtra<GoodsDetail.Detail>("detail_list")!!
        goods_id = intent.getStringExtra("goods_id")!!
    }

    override fun initData() {
        getData()
    }

    override fun observerUI() {
        viewModel.callUser.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    binding.userInfo = it.data
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
//                binding.refreshLayout.isRefreshing = false
            }

        })
        viewModel.tradeGoodsDetail.observe(this, {
            it?.let {
                if (it.isSuccess && it.data != null) {
                    binding.emptyLayout.visibility = View.GONE
                    binding.bean = it.data
                    tGoodsDetail = it.data

//                    tGoodsDetail?.goods_dict?.let {
//                        mImgLoader.load(it.goods_img).into(binding.ivGood)
//                    }

                    it.message?.let {
                        toast(it)
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.buyRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast(it.message)
                    getData()
                }
            }
        })
    }

    private fun getData() {
        viewModel.userInfo()
        viewModel.holdGoodsDetailCall(goods_id!!)

    }


    //查看商品详情
    fun goDetail() {

//        binding.bean?.let {
//            startActivity(Intent(this, PpZrDetailActivity::class.java).apply {
//                putExtra("banner_list", bannerList)
//                putExtra("detail_list", detailList)
//            })
//        }

    }

    //当前价购买 / 特价购买
    fun goDifTypeBuy(type: Int) {
        when (type) {
            1 -> {//特价购
                tGoodsDetail?.let {
                    JyInputNumDialog(this).apply {
                        ok = { num ->
                            viewModel.holdBuyTj(it, num)
                            dismiss()
                        }
                    }.show()
                }
            }
            2 -> {//优惠购
                tGoodsDetail?.let {
                    JyInputNumDialog(this).apply {
                        ok = { num ->
                            viewModel.holdBuyYh(it, num)
                            dismiss()
                        }
                    }.show()
                }
            }
            else -> {//当前购
                tGoodsDetail?.let {
                    JyInputNumDialog(this).apply {
                        ok = { num ->
                            viewModel.holdBuyYj(it, num)
                            dismiss()
                        }
                    }.show()
                }

            }
        }

        /* startActivity(Intent(this, XyzrNewActivity::class.java).apply {
             putExtra("index", 1)
             putExtra("titleName", "")

         })*/

    }


    //特价券 明细
    fun goLots1(num: String) {
//            startActivity(Intent(this, XyzrNewActivity::class.java).apply {
//                putExtra("detail_type", "20")//r_label还不确定
//            })

        tGoodsDetail?.goods_dict?.let {
        }
    }

    //优惠券 明细
    fun goLots2(num: String) {
        tGoodsDetail?.goods_dict?.let {
//            startActivity(Intent(this, XyzrNewActivity::class.java).apply {
//                putExtra("detail_type", "20")//r_label还不确定
//            })

        }
    }


    //通用明细
    fun goZrLots(num: String, pick_type: String, title_name: String, r_label: Int) {
        binding.bean?.let {
            startActivity(Intent(this, ZrLotsActivity::class.java).apply {
                putExtra("goods_id",goods_id)
                putExtra("goods_name", it.goods_dict.name)
                putExtra("banner_list", bannerList)
                putExtra("detail_list", detailList)
                putExtra("deal_num", num)
                putExtra("pick_type", pick_type)
                putExtra("title_name", title_name)
                putExtra("r_label", r_label)

//                //预支点对点
//                putExtra("goods_price", it.price)
//                //资产二
//                putExtra("price_pf", it.price_pf)
//                //可交易商品
//                putExtra("f_price", it.f_price)
//                putExtra("g_price", it.g_price)


            })
        }

    }


    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(meBroadcast)
    }

}